package com.tailyou.androidlearning202400.act

import android.os.Bundle
import android.view.View
import android.widget.AdapterView
import android.widget.ArrayAdapter
import android.widget.RatingBar
import android.widget.Spinner
import android.widget.TextView
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import com.tailyou.androidlearning202400.R
import com.tailyou.androidlearning202400.util.ToastUtil

class HealthyActivity : AppCompatActivity() {

    private lateinit var spinner: Spinner
    private lateinit var ratingBar: RatingBar

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContentView(R.layout.activity_healthy)
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
            insets
        }
        findViewById<TextView>(R.id.txtMarquee).isSelected = true
        // 找到Spinner组件
        spinner = findViewById(R.id.spinnerJCS)
        // 创建适配器，封装数据和布局文件
        val adapter: ArrayAdapter<CharSequence> = ArrayAdapter.createFromResource(
            this,
            R.array.jcs_array,
            android.R.layout.simple_spinner_item
        )
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
        // Spinner绑定适配器
        spinner.adapter = adapter
        // 设置选择监听器
        spinner.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
            override fun onItemSelected(p0: AdapterView<*>?, p1: View?, p2: Int, p3: Long) {
                ToastUtil.showToast(this@HealthyActivity, p0?.getItemAtPosition(p2).toString())
            }

            override fun onNothingSelected(p0: AdapterView<*>?) {

            }
        }

        // 找到RatingBar组件
        ratingBar = findViewById(R.id.ratingBar)
        // 绑定监听器，以便拿到评分数值
        ratingBar.onRatingBarChangeListener = RatingBar.OnRatingBarChangeListener { _, p1, _ ->
            // p1为评分值
            ToastUtil.showToast(
                this@HealthyActivity,
                p1.toString()
            )
        }
    }
}